package gui.utils;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;

import data.visu.utils.ImageLoader;

/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public class RadioButtonIcon implements Icon {
    public static final int WIDTH  = 45;
    public static final int HEIGHT = 45;
    
    private ImageIcon checked;
    private ImageIcon unchecked;
    private ImageIcon rollover;
    
    private boolean scale;
    
    public RadioButtonIcon(String name, boolean scale){
    	this.scale=scale;
    	checked = ImageLoader.createImageIcon(name+"_checked.PNG");
    	unchecked = ImageLoader.createImageIcon(name+"_unchecked.PNG");
    	rollover = ImageLoader.createImageIcon(name+"_rollover.PNG");
    }

    public int getIconWidth() {return WIDTH;}

    public int getIconHeight() {return HEIGHT;}


	@Override
	public void paintIcon(Component comp, Graphics g, int x, int y) {
	       AbstractButton btn = (AbstractButton)comp;
	       ButtonModel model = btn.getModel();
	       Graphics2D g2d = (Graphics2D)g;
	       if(scale){
	       
	       double heightRatio = (comp.getHeight()-10)/ (double)(checked.getIconHeight());
	       double widthRatio = (comp.getWidth()-10)/(double)(checked.getIconWidth());
	       double ratio = Math.min(heightRatio, widthRatio);
	       g2d.scale(ratio, ratio);
	       }

	       ImageIcon ii = new ImageIcon();
	       if(model.isSelected()){
	    	   ii = checked;
	       }
	       else{
	    	   if(model.isRollover()){
	    		   ii = rollover;
	    	   }
	    	   else{
	    		   ii = unchecked;
	    	   }
	       }
	       g2d.drawImage(ii.getImage(), x, y, comp);
	}
}
